package gamer.servlet;

import gamer.data.DatabaseOpera;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;

public class GameRegister extends HttpServlet {
    HashMap<String, String> u = new HashMap<>();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DatabaseOpera db = new DatabaseOpera();
        String nickname = req.getParameter("nickname");
        String user_id = req.getParameter("user_id");
        String pwd = req.getParameter("pwd");
        if(nickname==null||user_id==null||pwd==null){
            req.setAttribute("msg", "请输入完整信息");
            req.getRequestDispatcher("jsp/gamer/user/register.jsp").forward(req, resp);
        }
        u = db.mysqlQuery("*", "user", "user_id='" + user_id + "'");
        if (u.get("user_id") == null) {
            if (db.mysqlInsert("user", "nickname,user_id,pwd,money", "'"+nickname+ "','"+ user_id + "','" + pwd + "','10'"))
                System.out.println("注册成功");
            else System.out.println("注册失败");
        } else {
            System.out.println("帐户已存在");
        }
        req.getRequestDispatcher("jsp/gamer/user/login.jsp").forward(req, resp);
    }
}
